package com.itic.mall.mobile.users.complaint;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.mall.mobile.base.BaseMobileInterface;
import com.itic.mall.mobile.users.info.UserComplaintsVo;
import com.itic.mall.system.users.complaint.service.UserComplaintService;
import com.itic.mall.system.users.usersmall.persistence.model.UsersMall;
import com.itic.mall.system.users.complaint.persistence.model.UserComplaint;
import com.itic.mall.system.users.complaint.persistence.model.UserComplaintExample;
import com.itic.mall.system.users.feedback.service.UsersFeedbackService;
import com.itic.mall.system.users.feedback.persistence.model.UsersFeedback;
import com.itic.mall.system.users.feedback.persistence.model.UsersFeedbackExample;
import com.itic.mall.system.users.feedback.persistence.model.UsersFeedbackExample.Criteria;

/**
 * 投诉反馈接口
 * @time 2017-12-15
 * @author Administrator
 *
 */
@Controller
@RequestMapping(value="mobile/users/complaint")
public class UsersComplaintInterface extends BaseMobileInterface{
	@Autowired 
	private UserComplaintService userComplaintService;
	@Autowired 
	private UsersFeedbackService usersFeedbackService;
	
	/**
	 * 提交投诉建议
	 * @param request
	 * @param response
	 * @param token
	 * @param appId
	 * @return
	 */
	@RequestMapping(value="/submit")
    @ResponseBody
    public Map<String,Object> submit(HttpServletRequest request,HttpServletResponse response,String token,String appId,String content,String img,String reason,String phone,String name){
		String errorCode = PARAM_ERROR_CODE;
	   	String errorMsg = PARAM_ERROR_MSG;
	    Map<String, Object> resultMap = new HashMap<String,Object>();
	    try{
	    	if(StringHelper.isNotBlank(content) && StringHelper.isNotBlank(reason) && StringHelper.isNotBlank(token) && StringHelper.isNotBlank(appId)){
	    		UsersMall usersMall = getUsersByTokenAndAppId(token,appId);
	    		if(usersMall != null){
	    			String userId = usersMall.getId();
	    			String userNickName = usersMall.getUserNickName();
	    			String userPhone = usersMall.getUserPhone();
	    			UserComplaint userComplaint = new UserComplaint();
	    			userComplaint.setUserId(userId);
	    			if(name!=null && name.length()>0)
	    				userComplaint.setUserNickName(name);
	    			else
	    				userComplaint.setUserNickName(userNickName);
	    			userComplaint.setContent(content);
	    			userComplaint.setReason(reason);
	    			if(phone!=null && phone.length()>0)
	    				userComplaint.setUserPhone(phone);
	    			else
	    				userComplaint.setUserPhone(userPhone);
	    			userComplaint.setIsFinished(DBConst.FALSE);
	    			String result = userComplaintService.add(userComplaint);
	    			if(SUCCESS.equals(result)){
	    				UserComplaintsVo Vo = new UserComplaintsVo();
	    				Vo.setUserNickName(usersMall.getUserNickName());
	    				Vo.setUserPhone(usersMall.getUserPhone());
	    				resultMap.put(DATA, Vo);
		    			errorCode = SUCCESS_ERROR_CODE;
						errorMsg = SUCCESS_ERROR_MSG;
	    			}
	    		}else{
	    			errorCode = LOGIN_ERROR_CODE;
		   			errorMsg = LOGIN_ERROR_MSG;
	    		}
	    	}
	    }catch(Exception e){
	    	errorCode = SERVER_ERROR_CODE;
	       	errorMsg = SERVER_ERROR_MSG;
	   		logException("UsersComplaintInterface", "submit", e.toString(),"投诉提交接口",e);
	    }
	    resultMap.put(CODE, errorCode);
	    resultMap.put(MSG, errorMsg);
	    return resultMap;
	    }
	/**
	 * 投诉接口
	 * @param request
	 * @param response
	 * @param token
	 * @param appId
	 * @param content
	 * @return
	 */
	@RequestMapping(value="/add")
    @ResponseBody
    public Map<String,Object> add(HttpServletRequest request,HttpServletResponse response,String token,String appId,String content,String img){
		String errorCode = PARAM_ERROR_CODE;
	   	String errorMsg = PARAM_ERROR_MSG;
	    Map<String, Object> resultMap = new HashMap<String,Object>();
	    try{
	    	if(StringHelper.isNotBlank(token) && StringHelper.isNotBlank(appId) 
	    			&& (StringHelper.isNotBlank(content) || StringHelper.isNotBlank(img))){
	    		UsersMall usersMall = getUsersByTokenAndAppId(token,appId);
	    		if(usersMall != null){
	    			UserComplaint userComplaint = new UserComplaint();
	    			userComplaint.setUserId(usersMall.getId());
	    			userComplaint.setUserNickName(usersMall.getUserNickName());
	    			if(StringHelper.isNotBlank(content)){
		    			userComplaint.setContent(content);
	    			}
	    			if(StringHelper.isNotBlank(img)){
	    				userComplaint.setImg(img);
	    			}
	    			String result = userComplaintService.add(userComplaint);
	    			if(SUCCESS.equals(result)){
	    				//若投诉建议上传成功则立即给用户返回信息
	    				Date date = new Date();
	    	    		UsersFeedbackExample example = new UsersFeedbackExample();
	    	    		Criteria criteria = example.createCriteria();
	    	    		criteria.andIsEffectiveEqualTo(DBConst.TRUE);
	    	    		criteria.andStarttimeLessThan(date);
	    	    		criteria.andEndtimeGreaterThan(date);
	    	    		List<UsersFeedback> list = usersFeedbackService.list(example);
	    	    		if(list.size()>0){
	    	    			errorCode = SUCCESS_ERROR_CODE;
	    					errorMsg = SUCCESS_ERROR_MSG;
	    					resultMap.put("feedback", list.get(0).getContent());
	    	    		}else{
	    	    			errorCode = COMPLAINT_FEEDBACK_ERROR_CODE;
	    	    			errorMsg = COMPLAINT_FEEDBACK_ERROR_MSG;
	    	    		}
	    			}
	    		}else{
		   			errorCode = LOGIN_ERROR_CODE;
		   			errorMsg = LOGIN_ERROR_MSG;
		   		}
	    	}
	    }catch(Exception e){
	    	errorCode = SERVER_ERROR_CODE;
	       	errorMsg = SERVER_ERROR_MSG;
	   		logException("UsersComplaintInterface", "add", e.toString(),"投诉接口",e);
	    }
	    resultMap.put(CODE, errorCode);
	    resultMap.put(MSG, errorMsg);
	    return resultMap;
	    }
	/**
	 * 反馈接口
	 * @param request
	 * @param response
	 * @param content
	 * @param img
	 * @return
	 */
	@RequestMapping(value="/feedback")
    @ResponseBody
    public Map<String,Object> feedback(HttpServletRequest request,HttpServletResponse response,String content,String img){
		String errorCode = PARAM_ERROR_CODE;
	   	String errorMsg = PARAM_ERROR_MSG;
	    Map<String, Object> resultMap = new HashMap<String,Object>();
	    try{
	    	if(StringHelper.isNotBlank(content) || StringHelper.isNotBlank(img)) {
	    		Date date = new Date();
	    		UsersFeedbackExample example = new UsersFeedbackExample();
	    		Criteria criteria = example.createCriteria();
	    		criteria.andIsEffectiveEqualTo(DBConst.TRUE);
	    		criteria.andStarttimeLessThan(date);
	    		criteria.andEndtimeGreaterThan(date);
	    		List<UsersFeedback> list = usersFeedbackService.list(example);
	    		if(list.size()>0){
	    			errorCode = SUCCESS_ERROR_CODE;
					errorMsg = SUCCESS_ERROR_MSG;
					resultMap.put(DATA, list.get(0).getContent());
	    		}else{
	    			errorCode = COMPLAINT_FEEDBACK_ERROR_CODE;
	    			errorMsg = COMPLAINT_FEEDBACK_ERROR_MSG;
	    		}
	    	}
	    }catch(Exception e){
	    	errorCode = SERVER_ERROR_CODE;
	       	errorMsg = SERVER_ERROR_MSG;
	   		logException("UsersComplaintInterface", "feedback", e.toString(),"反馈接口",e);
	    }
	    resultMap.put(CODE, errorCode);
	    resultMap.put(MSG, errorMsg);
	    return resultMap;
	    }
}